package com.sunchangming.rpcgen;

public class TypeBean extends Type {
	private final UserType b;
	private final boolean defaultToNull;

	public TypeBean(UserType b, boolean defaultToNull) {
		super();
		this.b = b;
		this.defaultToNull = defaultToNull;
	}

	String getFullName() {
		return b.parent.getFullName() + "." + b.name;
	}

	@Override
	public String getName(Variable variable) {
		return this.getFullName();
	}

	@Override
	public String getMarshalStr(Variable v, String osname) {
		return String.format("%s=%s.marshal(%s)", osname, v.getName(), osname);
	}

	@Override
	public String getUnmarshalStr(Variable v, String osname, boolean useThis) {
		return String.format("%s=%s.createFromDataInputStream(%s)", v.getName(), this.getFullName(), osname);
	}

	@Override
	public String getBoxName() {
		return this.getFullName();
	}

	@Override
	public String getDefaultValue(Variable variable) {
		if (defaultToNull)
			return "null";
		else
			return "new " + getFullName() + "()";
	}

	@Override
	public String getHashCodeStr(Variable variable) {
		return "this." + variable.getName() + ".hashCode()";
	}

	@Override
	public String getFromSqlStr(Variable variable, String string) {
		return "this." + variable.getName() + "=new " + this.getFullName() + "(" + string + ");";
	}

}
